#include "ssicloud_treasure.h"


ssicloud::TreasureType::TreasureType()
    : ListObject<TreasureTypeLNode>(LQ_BASE_URL "/treasureType/query"
                           , OBJECT_NAME_TreasureType
                           , NetUrl::NET_HTTP_METHOD_GET)
{
    mUrl.appendHeader(HEADER_JSON);
    mUrl.appendHeader(HEADER_TOKEN);
}

bool ssicloud::TreasureType::requestAll()
{
    return obtain();
}

void ssicloud::TreasureType::profile()
{
    ListTable<TreasureTypeLNode>::iterator it = mNodes.begin();
    GEN_Printf(LOG_INFO, "---------用车宝典-获取宝典类型-------");

    for (; it != mNodes.end(); ++it)
    {
         GEN_Printf(LOG_DUMP, "ID:%s, 图片ID:%s, 类型名称:%s, 备注:%s, 创建时间:%s",
                    it->id.string(), it->fileId.c_str(), it->name.c_str(), it->remark.c_str(), it->createDate.c_str());
    }
    GEN_Printf(LOG_INFO, "---------完成-------");
}

int ssicloud::TreasureType::genResult(NetBuffer *data)
{
    int ret = KL_DATA_PRISER_OK;
    GEN_Printf(LOG_DEBUG, "size: %lu\n%s", data->size(), (char *)data->buffer());
    cJSON *root = cJSON_Parse((char *)data->buffer(), data->size());
    cJSON *dataJson = cJSON_GetObjectItem(root, "data");
    if (dataJson)
    {
        for (cJSON *item = dataJson->child; NULL != item; item = item->next )
        {
            TreasureTypeLNode tmp;

            JSON_VALUETRING_SWAP_BYTESTRING(item, id, tmp.id);
            JSON_VALUETRING_SWAP_BYTESTRING(item, fileId, tmp.fileId);
            JSON_VALUETRING_SWAP_BYTESTRING(item, name, tmp.name);
            JSON_VALUETRING_SWAP_BYTESTRING(item, remark, tmp.remark);
            JSON_VALUETRING_SWAP_BYTESTRING(item, createDate, tmp.createDate);

            // GEN_Printf(LOG_DEBUG, "items size=%d", tmp.items.size());
            mNodes.push_back(tmp);
        }

        if (mNodes.empty())
        {
            GEN_Printf(LOG_WARN, "load Category is empty.");
            ret = KL_DATA_PRISER_EMPTY;
        } else
        {
            profile();
        }
    } else
    {
        GEN_Printf(LOG_ERROR, "priser failed, size: %lu\n%s", data->size(), data->buffer());
        ret = KL_DATA_PRISER_JSOC_ERROR;
    }

    cJSON_Delete(root);
    return ret;
}

ssicloud::TreasureCar::TreasureCar()
    : ListObject<TreasureCarLNode>(LQ_BASE_URL "/treasureCar/page"
                           , OBJECT_NAME_TreasureCar
                           , NetUrl::NET_HTTP_METHOD_GET)
{
    mUrl.appendHeader(HEADER_JSON);
    mUrl.appendHeader(HEADER_TOKEN);

    mUrl.append("status", "1");
}

bool ssicloud::TreasureCar::requestAll(const char *title, int pageNum, int pageSize)
{
    if (isLoading()) return true;

    m_title = title;
    m_pageNum = SFString::number(pageNum);
    m_pageSize = SFString::number(pageSize);

    mUrl.appendChange("title", m_title.c_str());
    mUrl.appendChange("pageNum", m_pageNum.c_str());
    mUrl.appendChange("pageSize", m_pageSize.c_str());

    return obtain();
}

void ssicloud::TreasureCar::profile()
{
    ListTable<TreasureCarLNode>::iterator it = mNodes.begin();
    GEN_Printf(LOG_INFO, "---------用车宝典-分页查询-------");

    for (; it != mNodes.end(); ++it)
    {
         // GEN_Printf(LOG_DUMP, "ID:%s, 图片ID:%s, 类型名称:%s, 备注:%s, 创建时间:%s", it->id.string(), it->fileId.c_str(), it->name.c_str(), it->remark.c_str(), it->createDate.c_str());
    }
    GEN_Printf(LOG_INFO, "---------完成-------");
}

int ssicloud::TreasureCar::genResult(NetBuffer *data)
{
    int ret = KL_DATA_PRISER_OK;
    GEN_Printf(LOG_DEBUG, "size: %lu\n%s", data->size(), (char *)data->buffer());
    cJSON *root = cJSON_Parse((char *)data->buffer(), data->size());
    cJSON *dataJson = cJSON_GetObjectItem(root, "data");
    if (dataJson)
    {
        for (cJSON *item = dataJson->child; NULL != item; item = item->next )
        {
            TreasureCarLNode tmp;

//            JSON_VALUETRING_SWAP_BYTESTRING(item, id, tmp.id);
//            JSON_VALUETRING_SWAP_BYTESTRING(item, fileId, tmp.fileId);
//            JSON_VALUETRING_SWAP_BYTESTRING(item, name, tmp.name);
//            JSON_VALUETRING_SWAP_BYTESTRING(item, remark, tmp.remark);
//            JSON_VALUETRING_SWAP_BYTESTRING(item, createDate, tmp.createDate);

            // GEN_Printf(LOG_DEBUG, "items size=%d", tmp.items.size());
            mNodes.push_back(tmp);
        }

        if (mNodes.empty())
        {
            GEN_Printf(LOG_WARN, "load Category is empty.");
            ret = KL_DATA_PRISER_EMPTY;
        } else
        {
            profile();
        }
    } else
    {
        GEN_Printf(LOG_ERROR, "priser failed, size: %lu\n%s", data->size(), data->buffer());
        ret = KL_DATA_PRISER_JSOC_ERROR;
    }

    cJSON_Delete(root);
    return ret;
}
